package com.example.springbootdemo.filter;

///**
// * 登录认证的filter
// * 此过滤器接口一旦认证成功，则会调用AuthenticationSuccessHandler进行处理
// * @author hh
// * @date 2022/1/27
// */
//@Slf4j
//public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
//
//    private final UserDetailsService userDetailsService;
//
//    public JwtAuthenticationTokenFilter(@Qualifier("customUserDetailsService") UserDetailsService userDetailsService) {
//        this.userDetailsService = userDetailsService;
//    }
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String authHeader = request.getHeader(JwtTokenUtil.TOKEN_HEADER);
//        String username = null;
//        String authToken = null;
//        if (authHeader != null && authHeader.startsWith("Bearer ")){
//            authToken = authHeader.substring(7);
//            username = JwtTokenUtil.getUsernameFromToken(authToken);
//            log.info("从token中解析到用户名[{}]", username);
//        }
//
//        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null){
//            UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
//            if (JwtTokenUtil.validateToken(authToken, userDetails.getUsername())){
//                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                SecurityContextHolder.getContext().setAuthentication(authentication);
//            }
//        }
//
//        filterChain.doFilter(request, response);
//    }
//
//}
